package com.sls.nzdapplication.db.utils;

import android.content.Context;

import com.alibaba.fastjson.JSONArray;
import com.sls.nzdapplication.app.MySharedPreferences;
import com.sls.nzdapplication.db.DaoManager;
import com.sls.nzdapplication.db.DaoSession;
import com.sls.nzdapplication.db.DyEquipmentLineDao;
import com.sls.nzdapplication.model.DyEquipmentLine;
import com.sls.nzdapplication.model.EquipmentLine;


import org.greenrobot.greendao.query.QueryBuilder;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class DyEquipmentLineUtil {
    private static final boolean DUBUG = true;
    private DaoManager manager;
    private EquipmentLine equipmentLine;
    private DaoSession daoSession;
    private MySharedPreferences mySharedPreferences;
    private String username = "";

    public DyEquipmentLineUtil(Context context){
        manager = DaoManager.getInstance();
        manager.init(context);
        daoSession = manager.getDaoSession();
        manager.setDebug(DUBUG);
        mySharedPreferences = new MySharedPreferences(context);
        username = mySharedPreferences.getUserValue("username");
    }

    /**
     * 添加数据，如果有重复则覆盖
     */
    public void insertDyEquipmentLine(DyEquipmentLine equipmentLine) {
        equipmentLine.setCreateBy(username);
        equipmentLine.setCreateTime(new Date());
        manager.getDaoSession().insertOrReplace(equipmentLine);
    }


    /**
     * 添加多条数据，需要开辟新的线程
     */
    public void insertMultDyEquipmentLine(final List<DyEquipmentLine> equipmentLines) {
        manager.getDaoSession().runInTx(new Runnable() {
            @Override
            public void run() {
                for (DyEquipmentLine equipmentLine : equipmentLines) {
                    equipmentLine.setCreateBy(username);
                    equipmentLine.setCreateTime(new Date());
                    manager.getDaoSession().insertOrReplace(equipmentLine);
                }
            }
        });
    }

    /**
     * 添加多条数据，需要开辟新的线程
     */
    public void insertMultDyEquipmentLine2(final List<DyEquipmentLine> equipmentLines) {
        manager.getDaoSession().runInTx(new Runnable() {
            @Override
            public void run() {
                for (DyEquipmentLine equipmentLine : equipmentLines) {
                    manager.getDaoSession().insertOrReplace(equipmentLine);
                }
            }
        });
    }

    /**
     * 删除数据
     */
    public void deleteDyEquipmentLine(DyEquipmentLine equipmentLine) {
        manager.getDaoSession().delete(equipmentLine);
    }

    /**
     * 删除全部数据
     */
    public void deleteAll(Class cls) {
        manager.getDaoSession().deleteAll(cls);
    }

    /**
     * 更新数据
     */
    public void updateDyEquipmentLine(DyEquipmentLine equipmentLine) {
        equipmentLine.setModifyBy(username);
        equipmentLine.setModifyTime(new Date());
        manager.getDaoSession().update(equipmentLine);
    }

    /**
     * 按照主键返回单条数据
     */
    public DyEquipmentLine listOneDyEquipmentLine(String key) {
        return manager.getDaoSession().load(DyEquipmentLine.class, key);
    }

    public List<DyEquipmentLine> queryEquipmentLineByLineId(String id){
        QueryBuilder<DyEquipmentLine> builder = manager.getDaoSession().queryBuilder(DyEquipmentLine.class);
        List<DyEquipmentLine> equipmentLineList = builder.where(DyEquipmentLineDao.Properties.LineId.eq(id)).list();
        return  equipmentLineList;
    }

    public List<DyEquipmentLine> queryEquipmentLineByLineIdNoBox(String id){
        QueryBuilder<DyEquipmentLine> builder = manager.getDaoSession().queryBuilder(DyEquipmentLine.class);
        List<DyEquipmentLine> equipmentLineList = builder.where(builder.and(DyEquipmentLineDao.Properties.LineId.eq(id),builder.or(DyEquipmentLineDao.Properties.Type.notEq("1"),DyEquipmentLineDao.Properties.Type.notEq("2")))).orderAsc(DyEquipmentLineDao.Properties.CreateTime).list();
        return  equipmentLineList;
    }

    public List<DyEquipmentLine> queryEquipmentLineByStartAndEnd(String sid,String eid){
        QueryBuilder<DyEquipmentLine> builder = manager.getDaoSession().queryBuilder(DyEquipmentLine.class);
        List<DyEquipmentLine> equipmentLineList = builder.where(builder.and(DyEquipmentLineDao.Properties.StartEquipmentId.eq(sid),DyEquipmentLineDao.Properties.EndEquipmentId.eq(eid))).list();
        return  equipmentLineList;
    }

    public List<DyEquipmentLine> queryEquipmentLineByEid(String eid){
        QueryBuilder<DyEquipmentLine> builder = manager.getDaoSession().queryBuilder(DyEquipmentLine.class);
        List<DyEquipmentLine> equipmentLineList = builder.where(builder.or(DyEquipmentLineDao.Properties.StartEquipmentId.eq(eid),DyEquipmentLineDao.Properties.EndEquipmentId.eq(eid))).list();
        return  equipmentLineList;
    }


    public void deleteByAreaId(String id){
        List<DyEquipmentLine> items = queryEquipmentLineByLineId(id);
        for(DyEquipmentLine item :items){
            deleteDyEquipmentLine(item);
        }
    }


    /**
     * 查询全部数据
     */
    public List<DyEquipmentLine> queryAll() {
        return manager.getDaoSession().loadAll(DyEquipmentLine.class);
    }

    public JSONArray queryAllToJson(){
        JSONArray jsonArray = new JSONArray();
        List<DyEquipmentLine> equipmentLines = queryAll();
        for(DyEquipmentLine equipmentLine :equipmentLines){
            jsonArray.add(equipmentLine.toJson());
        }
        return jsonArray;
    }

    public void insertByJsArray(JSONArray jsonArray){
        List<DyEquipmentLine> list = new ArrayList<>();
        for(int i = 0;i<jsonArray.size();i++){
            list.add(new DyEquipmentLine(jsonArray.getJSONObject(i)));
        }
        insertMultDyEquipmentLine2(list);
    }
}
